package data;

import java.io.Serializable;
import java.util.Date;

/**
 * Data structure to hold Episode data.
 * @author Jen
 *
 */
public class Episode implements Serializable
{
	/**
	 * The serialnumber for Episode
	 */
	private static final long serialVersionUID = 01;

	/** the episode number (for the season it's a part of) */
	private int episodeNumber;
	
	/** the title of the episode */
	private String episodeTitle;
	
	/** the torrentURL */
	private String torrentURL = null;
	
	/** description of the episode */
	private String description = null;

	/** the date the episode aired */
	private Date airDate = null;
	
	/** determines if the show has been downloaded */
	private boolean hasDownloaded = false;

	/**
	 * Constructor
	 * @param epNum integer episode number
	 * @param epTitle string episode title
	 * @param torrentURL string torrent URL
	 * @param description string episode description
	 * @param airDate string the air date in Mon, 13 Jun 2011 03:00:28 -0500 form.
	 */
	public Episode(int epNum, String epTitle, String torrentURL, String description, Date airDate) 
	{
		this.episodeNumber = epNum;
		this.episodeTitle = epTitle;
		this.torrentURL = torrentURL;
		this.description = description;
		this.airDate = airDate;
	}
	
	public boolean setDownloaded(boolean bool)
	{
		return this.hasDownloaded = bool;
	}
		
	/**
	 * get's the episode title
	 * @return String episode title
	 */
	public String getTitle()
	{
		return this.episodeTitle;
	}
	
	/**
	 * get's the episode number
	 * @return integer episode number
	 */
	public int getEpisodeNumber()
	{
		return this.episodeNumber;
	}
	
	/**
	 * get's the episode title
	 * @return string episode title
	 */
	public String getEpisodeTitle()
	{
		return this.episodeTitle;
	}

	/**
	 * set's the torrent URL
	 * @param torrentURL string episode's new url
	 */
	public void setTorrentURL(String torrentURL) 
	{
		this.torrentURL = torrentURL;
	}

	/**
	 * get's the torrent URL
	 * @return string torrent URL
	 */
	public String getTorrentURL() 
	{
		String returnString;
		if(torrentURL == null)
		{
			returnString = null;
		}
		else if(torrentURL.equals("null"))
		{
			returnString = null;
		}
		else if(torrentURL.equals(""))
		{
			returnString = null;
		}
		else
		{
			returnString = torrentURL;
		}
		return returnString;
	}

	/**
	 * set's the description for the episode.
	 * @param description string the episodes new description.
	 */
	public void setDescription(String description) 
	{
		this.description = description;
	}
	
	/**
	 * get's the description from the episode
	 * @return string description 
	 */
	public String getDescription() 
	{
		String returnString;
		if(description == null)
		{
			returnString = null;
		}
		else if(description == "null")
		{
			returnString = null;
		}
		else if(description == "")
		{
			returnString = null;
		}
		else
		{
			returnString = description;
		}
		return returnString;
	}

	/**
	 * set's the air date
	 * @param string airDate the episode's new air date.
	 */
	public void setAirDate(Date airDate) 
	{
		this.airDate = airDate;
	}

	/**
	 * the date the episode aired
	 * @return Date airdate
	 */
	public Date getAirDate() 
	{
		return this.airDate;
		//return (Date)airDate.clone();
	}
	
	/**
	 * Returns a string representation of the episode.
	 * @return String representation of the episode.
	 */
	public String toString()
	{
		String returnString="";
		
		returnString += "Episode: "		+this.getEpisodeNumber()+" ";
		returnString += "Title: "		+this.getEpisodeTitle()	+" ";
		returnString += "URLs: "+this.getTorrentURL()	+" ";
		returnString += "Description: "	+this.getDescription()	+" ";
		returnString += "Air Date: "	+this.getAirDate()		+" ";
		
		return returnString;
	}
	
	/**
	 * Returns a XML string representation of the episode.
	 * @return String XML representation of the episode.
	 */
	public String toXML()
	{
		return this.toXML("");
	}
	
	/**
	 * Returns a XML string representation of the episode.
	 * @return String XML representation of the episode.
	 */
	public String toXML(String tabs)
	{
		String returnXML="";
			
		returnXML += tabs + "<episode>\n";
		
		tabs +="\t";
		
		returnXML += tabs + "<episodeNumber>"+this.getEpisodeNumber()+"</episodeNumber>\n";
		returnXML += tabs + "<title>"		+this.getEpisodeTitle()	+"</title>\n";
		returnXML += tabs + "<url>"			+this.getTorrentURL()	+"</url>\n";
		returnXML += tabs + "<description>"	+this.getDescription()	+"</description>\n";
		returnXML += tabs + "<hasDownloaded>";
		if(this.hasDownloaded())
		{
			returnXML += "true";
		}
		else
		{
			returnXML += "false";
		}
		returnXML += "</hasDownloaded>\n";
	

		
		//SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss Z");
		
		returnXML += tabs + "<airdate>"		+this.getAirDate()	+"</airdate>\n";

		tabs = tabs.replaceFirst("\t", "");

		returnXML += tabs + "</episode>\n";
			
		return returnXML.replaceAll("&", "&amp;");
	}


	public boolean hasDownloaded() 
	{
		return this.hasDownloaded;
	}

	public boolean merge(Episode b) 
	{
		boolean returnBool = false;
		if(this.getEpisodeTitle() == null && b.getEpisodeTitle() != null)
		{
			if(this.getEpisodeTitle() != b.getEpisodeTitle())
			{
				this.setEpisodeTitle(b.getEpisodeTitle());
				returnBool = true;
			}
		}
		if(this.getTorrentURL() == null &&b.getTorrentURL() != null)
		{
			if(this.getTorrentURL() != b.getTorrentURL())
			{
				this.setTorrentURL(b.getTorrentURL());
				returnBool = true;
			}
		}
		if(	this.getDescription() == null && b.getDescription() != null)
		{
			if(this.getDescription() != b.getDescription())
			{
				this.setDescription(b.getDescription());
				returnBool = true;
			}
		}
		if(this.getAirDate() == null && b.getAirDate() != null)
		{
			this.setAirDate(b.getAirDate());
			returnBool = true;
		}
		return returnBool;
	}

	private void setEpisodeTitle(String episodeTitle) 
	{
		this.episodeTitle = episodeTitle;
	}

	public boolean isValid() 
	{
		boolean returnBool = true;
		if(this.episodeNumber <= 0)
		{
			returnBool = false;
		}
		return returnBool;
	}

}
